package com.example.system.information.log;

import android.content.Context;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LogProcessor {
    private static final String TAG = "LogProcessor";
    private Context context;

    public LogProcessor(Context context) {
        this.context = context;
    }

    // 根据请求参数获取日志数据
    public JSONObject getLogData(String requestId, JSONArray logTypes, long startTime, long endTime, int maxCount) {
        JSONObject response = new JSONObject();
        try {
            response.put("requestId", requestId);

            // 处理每种日志类型
            JSONArray logsArray = new JSONArray();
            for (int i = 0; i < logTypes.length(); i++) {
                String logType = logTypes.getString(i);

                // 获取特定类型的日志
                List<LogEntry> logs = fetchLogsByType(logType, startTime, endTime, maxCount);

                // 将日志转换为JSON
                JSONArray logTypeArray = new JSONArray();
                for (LogEntry entry : logs) {
                    JSONObject logJson = new JSONObject();
                    logJson.put("timestamp", entry.getTimestamp());
                    logJson.put("type", entry.getType());
                    logJson.put("message", entry.getMessage());
                    logTypeArray.put(logJson);
                }

                // 将该类型的日志添加到响应中
                JSONObject typeLogs = new JSONObject();
                typeLogs.put("logType", logType);
                typeLogs.put("logs", logTypeArray);
                logsArray.put(typeLogs);
            }

            response.put("logs", logsArray);
            response.put("status", "success");
        } catch (JSONException e) {
            Log.e(TAG, "构建响应JSON失败: " + e.getMessage());
            try {
                response.put("status", "error");
                response.put("message", "构建响应失败: " + e.getMessage());
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }

        return response;
    }

    // 根据日志类型和时间范围获取日志
    private List<LogEntry> fetchLogsByType(String logType, long startTime, long endTime, int maxCount) {
        List<LogEntry> logs = new ArrayList<>();

        // 这里应该实现实际的日志获取逻辑
        // 示例中返回模拟数据，实际应用中应该从文件、数据库或系统日志中获取
        switch (logType) {
            case "error":
                logs = LogUtils.getInstance(context).getLogs("error", startTime, endTime, maxCount);
                break;
            case "network":
                logs = LogUtils.getInstance(context).getLogs("network", startTime, endTime, maxCount);
                break;
            case "performance":
                logs = LogUtils.getInstance(context).getLogs("performance", startTime, endTime, maxCount);
                break;
            case "ui":
                logs = LogUtils.getInstance(context).getLogs("ui", startTime, endTime, maxCount);
                break;
            case "database":
                logs = LogUtils.getInstance(context).getLogs("database", startTime, endTime, maxCount);
                break;
            case "security":
                logs = LogUtils.getInstance(context).getLogs("security", startTime, endTime, maxCount);
                break;
            default:
                Log.w(TAG, "未知的日志类型: " + logType);
        }

        return logs;
    }
}